﻿using System;
using System.Collections.Generic;
using System.Transactions;
using Common;
using Dto;
using Dto.Enum;
using SqlServerDAL;

namespace Business
{
    public class GroupTopicBusiness
    {
        private const string Waming = "Business.GroupTopicBusiness";
        private readonly GroupTopicDAL groupTopicDal = new GroupTopicDAL();
        private readonly TopicDetailsDAL topicDetailsDal = new TopicDetailsDAL();
        private readonly GroupManageDAL groupManageDal = new GroupManageDAL();

        #region CreateTopic[创建话题]
        /// <summary>
        /// 创建话题
        /// </summary>
        /// <param name="dto"></param>
        /// <param name="detailsDto"></param>
        /// <returns></returns>
        public int CreateTopic(GroupTopicDto dto, TopicDetailsDto detailsDto)
        {
            var tran = new TransactionScope();
            try
            {
                bool b;
                var topicId = groupTopicDal.Save(dto);
                detailsDto.TdTopicId = topicId;
                b = topicId > 0 && topicDetailsDal.Save(detailsDto);

                if (b)
                {
                    tran.Complete();
                    return topicId;
                }
                else
                {
                    return -1;
                }
            }
            catch (Exception ex)
            {
                Log.LogHandler.LogError(Waming + ".CreateTopic(GroupTopicDto dto, TopicDetailsDto detailsDto)", ex);
                return -1;
            }
            finally
            {
                tran.Dispose();
            }
        }
        #endregion

        public bool ReplyTopic(TopicDetailsDto topicDetailsDto)
        {
            var b = topicDetailsDal.Save(topicDetailsDto);
            return b;
        }

        public GroupTopicDto GetTopic(int id)
        {
            try
            {
                GroupTopicDto topicDto = null;
                var topicEntity = groupTopicDal.FindById(id);
                if (topicEntity != null)
                {
                    topicDto = FillDto.FillGroupTopicDto(topicEntity);
                }
                return topicDto;
            }
            catch (Exception ex)
            {
                Log.LogHandler.LogError(Waming + ".GetTopic(int id, int pageNo, int pageSize)", ex);
                return null;
            }
        }

        public List<TopicDetailsDto> GetDetails(int pageNo, int pageSize, int topicId, out int totalCount)
        {
            List<TopicDetailsDto> dtos = null;
            var hts = topicDetailsDal.SlicedFindAll((pageNo - 1) * pageSize + 1, pageSize, topicId, 0, "order by TdId");
            if (hts != null && hts.Count > 0)
            {
                var userBusiness = new UserBusiness();
                dtos = new List<TopicDetailsDto>();
                foreach (var ht in hts)
                {
                    var detailsDto = FillDto.FillTopicDetailsDto(ht);
                    var userDto = userBusiness.GetUsersById(detailsDto.TdUserId);
                    detailsDto.userDto = userDto;
                    int childCount;
                    detailsDto.ChildDtos = GetDetailsByParanId(pageNo, pageSize, topicId, detailsDto.TdId, out childCount);
                    dtos.Add(detailsDto);
                }
            }
            totalCount = topicDetailsDal.SelectCount(topicId, 0);
            return dtos;
        }

        public List<TopicDetailsDto> GetDetailsByParanId(int pageNo, int pageSize, int topicId, int paranId, out int totalCount)
        {
            List<TopicDetailsDto> dtos = null;
            var hts = topicDetailsDal.SlicedFindAll((pageNo - 1) * pageSize + 1, pageSize, topicId, paranId, "order by TdId");
            if (hts != null && hts.Count > 0)
            {
                var userBusiness = new UserBusiness();
                dtos = new List<TopicDetailsDto>();
                foreach (var ht in hts)
                {
                    var detailsDto = FillDto.FillTopicDetailsDto(ht);
                    var userDto = userBusiness.GetUsersById(detailsDto.TdUserId);
                    detailsDto.userDto = userDto;
                    dtos.Add(detailsDto);
                }
            }
            totalCount = topicDetailsDal.SelectCount(topicId, paranId);
            return dtos;
        }

        public List<GroupTopicDto> GetGroupTopics(int pageNo, int pageSize, int groupId, out int totalCount)
        {
            List<GroupTopicDto> dtos = null;
            var hts = groupTopicDal.SlicedFindAll((pageNo - 1) * pageSize + 1, pageSize * pageNo, groupId, "order by GtTime desc");
            if (hts != null && hts.Count > 0)
            {
                var userBusiness = new UserBusiness();
                dtos = new List<GroupTopicDto>();
                foreach (var ht in hts)
                {
                    var dto = FillDto.FillGroupTopicDto(ht);
                    var detailHt = topicDetailsDal.FindOne(dto.GtId);
                    if (detailHt != null)
                    {
                        dto.details = new List<TopicDetailsDto> { FillDto.FillTopicDetailsDto(detailHt) };
                    }
                    //创建人信息
                    dto.SponsorDto = userBusiness.GetUsersById(dto.GtSponsor);
                    dtos.Add(dto);
                }
            }
            totalCount = groupTopicDal.SelectCount(groupId);
            return dtos;
        }

        public bool GroupManagerRemoveTopic(int topicId, int managerId)
        {
            var tran = new TransactionScope();
            try
            {
                bool b;
                var topicDto = GetTopic(topicId);
                topicDto.GtState = 2;
                var id = groupTopicDal.Save(topicDto);
                if (id > 0)
                {
                    var groupManage = new GroupManageDto
                                          {
                                              ManageGroupId = topicDto.GtGroupId,
                                              ManageObjectId = topicDto.GtSponsor,
                                              ManageTargetId = topicDto.GtId,
                                              ManageTargetType = Convert.ToInt32(PublicEnum.GroupManageEnum.删除话题),
                                              ManageTime = DateTime.Now,
                                              ManageUserId = managerId
                                          };
                    b = groupManageDal.Save(groupManage);
                }
                else
                {
                    b = false;
                }

                if (b)
                {
                    tran.Complete();
                }
                return b;
            }
            catch (Exception ex)
            {
                Log.LogHandler.LogError(Waming + ".GroupManagerRemoveTopic(int topicId, int managerId)", ex);
                return false;
            }
            finally
            {
                tran.Dispose();
            }
        }
    }
}